#!/bin/bash

# Slackware build script for rename.pl

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# This replaces the former perl-rename from the SBo repo. The
# maintainer of perl-rename asked me to take over the build, and I
# decided I like the name "rename.pl" better. Also, the old one was in
# the perl/ category, implying it was a perl library. It's not. Just
# because an application is *written* in perl, doesn't mean it should
# be in the perl/ category (same goes for python/, people).

# This is a completely new SlackBuild (shares no code with
# perl-rename.SlackBuild).

# This is the "classic" rename, that used to ship with the perl source
# ages ago. It's also the same "rename" as Debian used to ship. I'm
# aware of the existence of https://metacpan.org/release/File-Rename
# but it's not the same: it's missing a lot of options (--no-stdin,
# -i, -l, -v, and all the options relating to backups). It also
# added some new options that do look useful (-0, -d, -e, -E), but it
# doesn't look like a good tradeoff to me.

# Since the name "rename" conflicts with util-linux, I've named this
# rename.pl, and gone to some trouble to make sure its documentation
# refers to rename.pl consistently (particularly the example commands,
# which wouldn't work if you copy/pasted them as-is).

# This version of rename is still actively maintained, as of 20221113.

# 20221228 bkw: updated for v1.12, fix PKGTYPE assignment.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=rename.pl
VERSION=${VERSION:-1.12}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

SRCNAM=rename

ARCH=noarch

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz
cd $SRCNAM-$VERSION
chown -R root:root .
chmod 644 bin/* *
chmod 755 . bin

# rename => rename.pl in embedded docs. Tricky because the word
# "rename" is also used as a normal verb and shouldn't be changed in
# that case.
sed -i \
  -e '/^rename - renames/s,rename,rename.pl,' \
  -e '/^  *rename /s,rename,rename.pl,' \
  -e 's,rename prompts,rename.pl prompts,g' \
  -e 's,<rename>,<rename.pl>,g' \
  -e '/make sure rename doesn.t/s,rename,B<rename.pl>,' \
  bin/$SRCNAM.PL

# Fix typos in embedded docs:
sed -i \
  -e 's,Us it,Use it,' \
  -e 's,mv prompts,<rename.pl> prompts,' \
  -e 's,fo details,for details,' \
  -e 's,insted,instead,' \
  -e 's,wheter,whether,' \
  -e 's,reame,rename,' \
  -e 's,<-no-stdin>,<--no-stdin>,' \
  -e 's,I<rename\.pl>,B<rename.pl>,' \
  -e 's, <rename\.pl>, B<rename.pl>,' \
  bin/$SRCNAM.PL

perl Makefile.PL
make

# Generated man page still calls itself RENAME.
sed -i '/^\./s,\<RENAME\>,&.PL,' blib/man1/$SRCNAM.1

# This really is just a perl script and a man page. 'make install'
# creates lib(64) dirs and a perllocal.pod that we don't need.
mkdir -p $PKG/usr/bin $PKG/usr/man/man1
install -m0755 bin/$SRCNAM $PKG/usr/bin/$PRGNAM
gzip -9c < blib/man1/$SRCNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a Changes README.md $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
